package com.manager.devicemanager.controller;

import com.manager.devicemanager.common.DataSourceNameEnum;
import com.manager.devicemanager.common.Result;
import com.manager.devicemanager.entity.Statusaccounting;
import com.manager.devicemanager.service.IStatusaccountingService;
import com.manager.devicemanager.vo.ComponentStatusRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *   器件状态统计数据 实时
 * </p>
 *
 * @author wpf
 * @since 2025-06-24
 */
@RestController
@RequestMapping("/statusaccounting")
public class StatusaccountingController {
    @Autowired
    private IStatusaccountingService statussAccountingService;


    /**
     * 获取 IC 板的最新设备状态
     * @return 实时设备状态对象
     */
    @PostMapping("/last")
    public Result<Statusaccounting> getDataLast(@RequestBody ComponentStatusRequestVO componentStatusRequestVO) {
        Statusaccounting dataLastIc = statussAccountingService.getDataLast("ic".equals(componentStatusRequestVO.getType())?DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex():DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex());
//        Statusaccounting dataLastic1 = statussAccountingService.getDataLastPd(DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName()+"1");
//        if (dataLastic1 != null) {
//            dataLastIc.setStatusCnt1(dataLastIc.getStatusCnt1()+dataLastic1.getStatusCnt1());
//            dataLastIc.setStatusCnt2(dataLastIc.getStatusCnt2()+dataLastic1.getStatusCnt2());
//            dataLastIc.setStatusCnt3(dataLastIc.getStatusCnt3()+dataLastic1.getStatusCnt3());
//            dataLastIc.setStatusCnt4(dataLastIc.getStatusCnt4()+dataLastic1.getStatusCnt4());
//            dataLastIc.setTotalDevices(dataLastIc.getTotalDevices()+dataLastic1.getTotalDevices());
//        }
        return Result.success(dataLastIc);
    }

    /**
     * 获取 PD 板的最新设备状态
     * @return 实时设备状态对象
     */

   @PostMapping("/lastweek")
    public Result<List<Statusaccounting>> getLastWeek(@RequestBody ComponentStatusRequestVO componentStatusRequestVO) {
       List<Statusaccounting> lastWeekIC = statussAccountingService.getLastWeek("ic".equals(componentStatusRequestVO.getType())?DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex():DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName() + componentStatusRequestVO.getDatasourceindex());
//       List<Statusaccounting> lastWeekIC1 = statussAccountingService.getLastWeekIC(DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName()+"1");
//       if (lastWeekIC1==null){
//       for (Statusaccounting statusaccounting : lastWeekIC) {
//           for (Statusaccounting statusaccounting1 : lastWeekIC1) {
//               if (statusaccounting.getTime().equals(statusaccounting1.getTime())) {
//                   statusaccounting.setStatusCnt1(statusaccounting.getStatusCnt1() + statusaccounting1.getStatusCnt1());
//                   statusaccounting.setStatusCnt2(statusaccounting.getStatusCnt2() + statusaccounting1.getStatusCnt2());
//                   statusaccounting.setStatusCnt3(statusaccounting.getStatusCnt3() + statusaccounting1.getStatusCnt3());
//                   statusaccounting.setStatusCnt4(statusaccounting.getStatusCnt4() + statusaccounting1.getStatusCnt4());
//                   statusaccounting.setTotalDevices(statusaccounting.getTotalDevices() + statusaccounting1.getTotalDevices());
//               }
//           }
//       }
//      }
       return Result.success(lastWeekIC);
    }

    @PostMapping("/lastsixmonth")
    public Result<List<Statusaccounting>> getLastSixMonths(@RequestBody ComponentStatusRequestVO componentStatusRequestVO){
        return Result.success(statussAccountingService.getLastSixMonths("ic".equals(componentStatusRequestVO.getType())?DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName()+componentStatusRequestVO.getDatasourceindex():DataSourceNameEnum.PDBOARDCONFIG.getDataSourceName()+componentStatusRequestVO.getDatasourceindex()));
    }

}
